package com.whl.sm.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.system.OrgUserInfoMapper;
import com.whl.sm.model.system.OrgUserInfo;
import com.whl.sm.service.system.OrgUserInfoService;
import com.whl.sm.service.system.OrgUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-09-10
 */
@Slf4j
@Service
public class OrgUserInfoServiceImpl extends ServiceImpl<OrgUserInfoMapper, OrgUserInfo> implements OrgUserInfoService {

    @Autowired
    private OrgUserService orgUserService;

    @Override
    public OrgUserInfo getByUserId(String userId) {
        OrgUserInfo orgUserInfo = getOne(new QueryWrapper<OrgUserInfo>().eq("user_id",userId));
        return orgUserInfo;
    }

    @Override
    public OrgUserInfo getByCurrentUser() {
        OrgUserInfo returnData = new OrgUserInfo();
        String currentUser = orgUserService.getCurrentUserId();
        if (Objects.nonNull(currentUser)){
            returnData = getByUserId(currentUser);
        }
        return returnData;
    }

    @Override
    public Result updateInfo(OrgUserInfo orgUserInfo) {
        try {
            if (Objects.isNull(orgUserInfo.getId())){
                String currentUser = orgUserService.getCurrentUserId();
                if (Objects.isNull(currentUser)){
                    return Result.fail();
                }
                orgUserInfo.setUserId(currentUser);
                getBaseMapper().insert(orgUserInfo);
            }else {
                getBaseMapper().updateById(orgUserInfo);
            }
        }catch (Exception e){
            log.error("保存个人信息异常:"+e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }
}
